package com.powerdata.system.paimon.impl;

import cn.hutool.core.util.ObjectUtil;
import com.powerdata.common.utils.SecurityUtils;
import com.powerdata.common.utils.StringUtils;
import com.powerdata.system.domain.PaimonExecLog;
import com.powerdata.system.domain.PaimonExecLogExample;
import com.powerdata.system.domain.PaimonSaveSql;
import com.powerdata.system.domain.PaimonSaveSqlExample;
import com.powerdata.system.domain.param.DelSaveSqlParam;
import com.powerdata.system.domain.param.PaimonExecLogParam;
import com.powerdata.system.domain.param.PaimonSaveSqlParam;
import com.powerdata.system.mapper.PaimonExecLogMapper;
import com.powerdata.system.mapper.PaimonSaveSqlMapper;
import com.powerdata.system.paimon.PaimonSaveSqlService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author deeprado
 * @version 1.0
 * @description
 * @date 2023/8/24 16:10
 */
@Service
public class PaimonSaveSqlServiceImpl implements PaimonSaveSqlService {
    @Resource
    private PaimonSaveSqlMapper paimonSaveSqlMapper;
    @Resource
    private PaimonExecLogMapper paimonExecLogMapper;

    @Override
    public Map<String, Object> sqlList(PaimonSaveSqlParam paimonSaveSqlParam) throws Exception {
        final HashMap<String, Object> result = new HashMap<>();
        int pageSize = ObjectUtils.isEmpty(paimonSaveSqlParam.getPageSize()) ? 10 : paimonSaveSqlParam.getPageSize();
        int pageNum = ObjectUtils.isEmpty(paimonSaveSqlParam.getPageNum()) ? 1 : paimonSaveSqlParam.getPageNum();
        String orderByColumn = StringUtils.isEmpty(paimonSaveSqlParam.getOrderByColumn()) ?
                "name" : paimonSaveSqlParam.getOrderByColumn();
        String isAsc = StringUtils.isEmpty(paimonSaveSqlParam.getIsAsc()) ? "desc" : paimonSaveSqlParam.getIsAsc();

        PaimonSaveSqlExample paimonSaveSqlExample = new PaimonSaveSqlExample();
        PaimonSaveSqlExample.Criteria criteria = paimonSaveSqlExample.createCriteria();

        if (StringUtils.isNotEmpty(paimonSaveSqlParam.getName())) {
            criteria.andNameLike("%" + paimonSaveSqlParam.getName() + "%");
        }
        if (StringUtils.isNotEmpty(paimonSaveSqlParam.getExecsql())) {
            criteria.andExecsqlLike("%" + paimonSaveSqlParam.getExecsql() + "%");
        }
        if (StringUtils.isNotEmpty(paimonSaveSqlParam.getCreater())) {
            criteria.andCreaterLike("%" + paimonSaveSqlParam.getCreater() + "%");
        }
        List<PaimonSaveSql> paimonSaveSqlList = paimonSaveSqlMapper.selectByExample(paimonSaveSqlExample);
        if (ObjectUtils.isEmpty(paimonSaveSqlList)) {
            result.put("total", 0);
            result.put("list", null);
            return result;
        }
        result.put("total", paimonSaveSqlList.size());

        paimonSaveSqlExample.setOrderByClause(" " + orderByColumn + " " + isAsc + " limit " + (pageNum - 1) * pageSize + "," + pageSize);
        result.put("list", paimonSaveSqlMapper.selectByExample(paimonSaveSqlExample));
        return result;
    }

    @Override
    public void add(PaimonSaveSql paimonSaveSql) throws Exception {
        if (ObjectUtil.isNotEmpty(paimonSaveSqlMapper.selectByPrimaryKey(paimonSaveSql.getName()))) {
            throw new Exception("sql名称已存在，请重新命名");
        }
        paimonSaveSql.setCreater(SecurityUtils.getLoginUser().getUsername());
        paimonSaveSql.setCreatetime(new Date().getTime() + "");
        String sql = paimonSaveSql.getExecsql().replaceAll("\n", " ");
        while (sql.contains("  ")) {
            sql = sql.replaceAll("  ", " ");
        }
        paimonSaveSql.setExecsql(sql.replaceAll(";", ";\n"));
        paimonSaveSqlMapper.insertSelective(paimonSaveSql);
    }

    @Override
    public void modify(PaimonSaveSql paimonSaveSql) throws Exception {
        paimonSaveSql.setCreater(SecurityUtils.getLoginUser().getUsername());
        paimonSaveSql.setCreatetime(new Date().getTime() + "");
        String sql = paimonSaveSql.getExecsql().replaceAll("\n", " ");
        while (sql.contains("  ")) {
            sql = sql.replaceAll("  ", " ");
        }
        paimonSaveSql.setExecsql(sql.replaceAll(";", ";\n"));
        paimonSaveSqlMapper.updateByPrimaryKey(paimonSaveSql);
    }

    @Override
    public void delete(DelSaveSqlParam delSaveSqlParam) throws Exception {
        PaimonSaveSqlExample paimonSaveSqlExample = new PaimonSaveSqlExample();
        paimonSaveSqlExample.createCriteria().andNameIn(delSaveSqlParam.getDelNames());
        paimonSaveSqlMapper.deleteByExample(paimonSaveSqlExample);
    }

    @Override
    public Map<String, Object> sqlHistoryList(PaimonExecLogParam paimonExecLogParam) throws Exception {
        HashMap<String, Object> result = new HashMap<>();
        int pageSize = ObjectUtils.isEmpty(paimonExecLogParam.getPageSize()) ? 10 : paimonExecLogParam.getPageSize();
        int pageNum = ObjectUtils.isEmpty(paimonExecLogParam.getPageNum()) ? 1 : paimonExecLogParam.getPageNum();
        String orderByColumn = StringUtils.isEmpty(paimonExecLogParam.getOrderByColumn()) ?
                "id" : paimonExecLogParam.getOrderByColumn();
        String isAsc = StringUtils.isEmpty(paimonExecLogParam.getIsAsc()) ? "desc" : paimonExecLogParam.getIsAsc();

        PaimonExecLogExample paimonExecLogExample = new PaimonExecLogExample();
        PaimonExecLogExample.Criteria criteria = paimonExecLogExample.createCriteria();

        if (StringUtils.isNotEmpty(paimonExecLogParam.getSqlstr())) {
            criteria.andSqlstrLike("%" + paimonExecLogParam.getSqlstr() + "%");
        }
        if (StringUtils.isNotEmpty(paimonExecLogParam.getStatus())) {
            criteria.andStatusEqualTo(paimonExecLogParam.getStatus());
        }
        if (StringUtils.isNotEmpty(paimonExecLogParam.getCreater())) {
            criteria.andCreaterLike("%" + paimonExecLogParam.getCreater() + "%");
        }
        List<PaimonExecLog> paimonSaveSqlList = paimonExecLogMapper.selectByExample(paimonExecLogExample);
        if (ObjectUtils.isEmpty(paimonSaveSqlList)) {
            result.put("total", 0);
            result.put("list", null);
            return result;
        }
        result.put("total", paimonSaveSqlList.size());

        paimonExecLogExample.setOrderByClause(" " + orderByColumn + " " + isAsc + " limit " + (pageNum - 1) * pageSize + "," + pageSize);
        result.put("list", paimonExecLogMapper.selectByExample(paimonExecLogExample));
        return result;
    }
}
